package com.czy.owner.ui.setting;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.widget.MyEditText;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.OnClick;

import static com.czy.owner.utils.PhoneUtils.ShowToastMessage;


/**
 * Created by wangyang on 2016/6/1.
 * 忘记支付密码
 */

public class ForgetPaidPasswordActivity extends BaseActivity {

    @BindView(R.id.et_piad_phone)
    MyEditText etPiadPhone;
    @BindView(R.id.et_piad_code)
    EditText etPiadCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_piad_psw)
    MyEditText etPiadPsw;
    @BindView(R.id.et_paid_psw_again)
    MyEditText etPiadPswAgain;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_forget_paid_password;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.forget_paid_psw);
        etPiadPhone.setText((String) SharedPreferencesUtils.getParam(ForgetPaidPasswordActivity.this, Constants.SHARED_PREFERENCES_KEY_PHONE, ""));
        etPiadPhone.setSelection(etPiadPhone.getText().toString().length());
    }

    @Override
    protected void updateViews() {

    }


    //获取验证码
    @OnClick(R.id.tv_get_code)
    public void tvGetGodeOnClick(View view) {

        String phone = etPiadPhone.getText().toString().trim();
        if (!PhoneUtils.isNetworkConnected(ForgetPaidPasswordActivity.this)) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, "网络连接错误!");
            return;
        }

        if (TextUtils.isEmpty(phone)) {
            ShowToastMessage(ForgetPaidPasswordActivity.this, getString(R.string.phone_error_null));
            return;
        }

        if (!PhoneUtils.isMobileNO(phone)) {
            ShowToastMessage(ForgetPaidPasswordActivity.this, getString(R.string.phone_error));
            return;
        }
        mLoadView.ShowLoadView();
        RequestParams params = new RequestParams(Constants.APP_API_BASE + Constants.OWN_REQSMSCODEPAYPASSWD);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(ForgetPaidPasswordActivity.this).getSession());
        params.addBodyParameter("phone", phone);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                String data = PhoneUtils.checkResponseFlag(ForgetPaidPasswordActivity.this, result);
                if (data != null) {
                    timer.start();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }
        });


    }


    @OnClick(R.id.btn_sure_set)
    public void btnSureOnClick(View view) {
        String phone = etPiadPhone.getText().toString().trim();
        String code = etPiadCode.getText().toString().trim();
        String paidPsw = etPiadPsw.getText().toString().trim();
        String paidPswAgain = etPiadPswAgain.getText().toString().trim();

        if (!PhoneUtils.isNetworkConnected(ForgetPaidPasswordActivity.this)) {
            return;
        }

        if (TextUtils.isEmpty(phone)) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, getString(R.string.phone_error_null));
            etPiadPhone.requestFocus();
            return;
        }
        if (!PhoneUtils.isMobileNO(phone)) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, getString(R.string.phone_error));
            etPiadPhone.requestFocus();
            return;
        }


        if (TextUtils.isEmpty(code)) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, getString(R.string.code_error_null));
            etPiadCode.requestFocus();
            return;
        }


        if (TextUtils.isEmpty(paidPsw)) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, "新的支付密码不能为空");
            etPiadPsw.requestFocus();
            return;
        }


        if (paidPsw.length() != 6) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, getString(R.string.paid_psw_length));
            etPiadPsw.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(paidPswAgain)) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, "再次输入密码不能为空");
            etPiadPswAgain.requestFocus();
            return;
        }

        if (paidPswAgain.length() != 6) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, getString(R.string.paid_psw_length_again));
            etPiadPswAgain.requestFocus();
            return;
        }

        if (!paidPsw.equals(paidPswAgain)) {
            PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, "两次输入的支付密码不一致");
            etPiadPswAgain.requestFocus();
            return;
        }

        sureGetApi(phone, code, paidPsw);
    }


    private void sureGetApi(String phone, String code, String psw) {
        mLoadView.ShowLoadView();
        String url = Constants.APP_API_BASE + Constants.OWN_SETPAYPASSWD;
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("session", UserHelper.getInstance().getSessionInfoModel(ForgetPaidPasswordActivity.this).getSession());
        params.addBodyParameter("userId", UserHelper.getInstance().getUserInfoModel(ForgetPaidPasswordActivity.this).getUserId() + "");
        params.addBodyParameter("phone", phone);
        params.addBodyParameter("smsCode", code);
        params.addBodyParameter("payPassword", psw);
        x.http().post(params, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                String data = PhoneUtils.checkResponseFlag(ForgetPaidPasswordActivity.this, result);
                if (data != null) {
                    PhoneUtils.ShowToastMessage(ForgetPaidPasswordActivity.this, "支付密码重置成功");
                    finish();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }


            @Override
            public void onFinished() {
                mLoadView.CancleLoadView();
            }

        });
    }


    //   倒计时响应事件  timer.start();
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            tvGetCode.setClickable(false);// 设置不能点击
            tvGetCode.setText(millisUntilFinished / 1000 + getString(R.string.send_again));// 设置倒计时时间
            tvGetCode.setTextColor(getResources().getColor(R.color.appTipColor)); // 设置为灰色，这时是不能点击的
        }

        @Override
        public void onFinish() {
            tvGetCode.setText(getResources().getString(R.string.get_yanzhengma));
            tvGetCode.setClickable(true);// 重新获得点击
            tvGetCode.setTextColor(getResources().getColor(R.color.appThemeColor));

        }
    };


}
